Back to course home
0% completed
Vote For New Content
Meta Hard Problems
Sliding Window Median (hard)
Solution: Sliding Window Median
Alien Dictionary (hard)
Solution: Alien Dictionary
Minimum Window Substring (hard)
Solution: Minimum Window Substring
Best Meeting Point (hard)
Solution: Best Meeting Point
Maximum Sum of 3 Non-Overlapping Subarrays (hard)
Solution: Maximum Sum of 3 Non-Overlapping Subarrays
Making A Large Island (hard)
Solution: Making A Large Island
Basic Calculator (Hard)
Solution: Basic Calculator
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Longest Increasing Path in a Matrix (hard)
Solution: Longest Increasing Path in a Matrix
Valid Palindrome III (hard)
Solution: Valid Palindrome III
Binary Tree Vertical Order Traversal (medium)
Problem Statement
Given the root of the binary tree, return the 2D list containing the vertical order traversal of the binary tree.
A vertical order traversal of the tree is defined as a top to bottom, column by column traversal.
Note: If two nodes are in the same row and column, keep its order from left to right.
Examples
Example 1:
- Input: A binary tree:
[1,2,3,4,5,6,7] - Expected Output:
[[4], [2], [1,5,6], [3], [7]] - Justification: Nodes 4, 2, 1 with 5 and 6, 3, and 7 are in separate vertical lines. The nodes in each vertical line are listed in the order they appear from top to bottom.
Example 2:
- Input: A binary tree:
[3,9,8,4,0,1,7] - Expected Output:
[[4], [9], [3,0,1], [8], [7]] - Justification: Nodes are grouped based on their vertical positions. Lower nodes in the same vertical line follow the higher ones.
Example 3:
- Input: A binary tree:
[3,null,20,15,7] - Expected Output:
[[3, 15], [20], [7]] - Justification: The tree is skewed to the right. The output reflects the vertical traversal from left to right.
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Custom Sort String
Solution: Binary Tree Vertical Order Traversal
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself